<?
ob_start(); 
include("common/header.html");
include("common/left.html");
include("common/page.php");
include("class/OrderRecord.php");//查询订单详情和处理订单的相关操作
include("class/ProOrder.php");//票付通平台会员的订单统计，查询操作
include("class/SimpleExcel.class.php");
$out_html=ob_get_contents();
ob_end_clean();
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s= new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$memberID=$_SESSION['sid'];

if(strlen($_SESSION['account'])==11) {
	$pOrder=buyProOrderObject($memberID,$s,1);
}else{
	$pOrder=buyProOrderObject($memberID,$s,$_SESSION['sdtype']);
}
$v['title'] ="已买到的产品";
$v['from']="buy";
$currentPage=$_REQUEST["currentPage"]?$_REQUEST["currentPage"]:1;
$pageSize=$_REQUEST['pageSize']?$_REQUEST["pageSize"]:15;

$concat=0;
// 导出全部订单
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export"){
	$currentPage=1;
	$pageSize=99999;
}
$r = $pOrder->_orderlist($currentPage,$pageSize,$concat);
if($_REQUEST['method']=='countAll'){
    $countAll = explode(',',$pOrder->totalOrders(2));
    echo json_encode($countAll);
    exit;
}

$orderCount = $ticketCount = $allMoney = 0;// 订单统计信息
//  获取产品对应的供应商列表
if(count($r)){
	$aids = array();
	foreach($r as $key=>$row){
		if(!isset($aids[(string)$row['mainOrder']->UUsellid])){
            //如果是套票的话 要重新取一次供应商
            if((string)$row['mainOrder']->UUconcat_id!=0){
               $x= simplexml_load_string($s->Order_Globle_Search('', $_SESSION['sid'], '', '', '', '', '', '', '', '', '', '', (string)$row['mainOrder']->UUordernum, '', '', '', '', '', '1', '1', '0', '1', '0', '0', '', '0', '', '0', '', '', '', '0', '0', '0'));
               $aids[(string)$x->Rec->UUsellid] = (string)$x->Rec->UUsellid;
            }else{
                $aids[(string)$row['mainOrder']->UUsellid]=(string)$row['mainOrder']->UUsellid;
            }
        }
		// 统计订单信息
		$orderCount++;
		$ticketCount+=(string)$row['mainOrder']->UUtnum;
		//$allMoney+=((int)$row['mainOrder']->UUtprice*(int)$row['mainOrder']->UUtnum)/100;
	}
    
	$str="select id,dname from pft_member where id IN (".implode(',',$aids).")";
	$anames=array();
	$GLOBALS['le']->query($str);
	while($row=$GLOBALS['le']->fetch_assoc())
		$anames[$row['id']]=$row['dname'];
}

$childOrder = $arr = array();
$tmp = $r;
$arr = array();
foreach($r as $key => $row){



	// 处理
	$concat = (string)$row['mainOrder']->UUconcat_id;
	$orderid = (string)$row['mainOrder']->UUordernum;

    //如果第一个订单就是联票 那么可能会有前几个联票的子票没取到 要回头取一下
    if($key=='0' && $concat!='0'){
        $max_r = count($r);
        $x= simplexml_load_string($s->Order_Globle_Search('', $_SESSION['sid'], '', '', '', '', '', '', '', '', '', '', $concat, '', '', '', '', '', '1', '1', '0', '5', '0', '0', '', '0', '', '0', '', '', '', '1', '0', '0'));

        foreach($x->Rec as $v){
            if((int)$v->UUordernum<=$orderid){
                break;
            }
            $r[$max_r] = array('mainOrder'=>$v);
            $tmp[$max_r]= $r[$max_r];
            ++$max_r;
        }
        if($_GET['csj']==1){
            echo "<pre>";
            print_r($r);print_r($tmp);
            echo "</pre>";
        }
    }

	if($concat==0){
		$arr[] = $r[$key];// 主订单
		continue;
	}
	if($concat!=$orderid){
		$tmp[$key]= $r[$key];
		continue;
	}
	// 主订单并且含有从订单
	foreach($tmp as $k=>$data){
		if((string)$data['mainOrder']->UUconcat_id==$orderid && (string)$data['mainOrder']->UUordernum!=$orderid){
			$r[$key]['childOrder'][] = $data;
			unset($tmp[$k]);
		}
	}
	$arr[] = $r[$key];
}
$r = $arr;

// 获取对应的供应商
include 'getDistors.php';
// if($_SESSION['memberID']!=1)
$supList = getDistors($GLOBALS['le']);

$dTypeName = '供应商';
$atype= 'buy';// 导出文件名判断条件
/* 给百合国旅查账
if($_SESSION['sid']==40142 && $_REQUEST['act']=="Export"){
    foreach($r as $row){
        if((int)$row['mainOrder']->UUpmode!=2) continue;
        $res[] = (int)$row['mainOrder']->UUordernum;
        if((int)$row['mainOrder']->UUconcat_id!=0){
            foreach($row['childOrder'] as $v){
                $res[] = (int)$v['mainOrder']->UUordernum;
            }
        }
    }
    echo '<pre>';
    var_export($res);
    echo '</pre>';
    exit;
}
*/

include("loadExcel_new.php");
echo $out_html;
// 判断订单是否为主订单  true为主订单
function isMainOrder($field){
	return ($field=='main' || $field!=0 ) ? true:false;
}
// 复票订单主订单
function echoMainOrder($obj){
	return ((String)$obj->UUconcat_id=='main') ? (String)$obj->UUordernum:(String)$obj->UUconcat_id;
}

$uuStatus = array(0=>"未使用",1=>"<em class='green'>已使用</em>",2=>"已过期",3=>"<em class='red'>已取消</em>",4=>"被替代",5=>"<em class='green'>已使用</em><br/><em class='red'>(被修改)</em>",6=>"已撤销",7=>"部分使用");
// 不显示分销商列
$showDtrArr = array(2223);
$showDtr = true;
if(in_array($_SESSION['sid'], $showDtrArr)){
	$showDtr = false;
}
//$time_mark =(strtotime($_GET['endtime']) - strtotime($_GET['begintime']))<5092000;//时间跨度大于两个月 则不显示统计信息 ,页数也用假的

$count=$pOrder->orderlistTotal();

$showTotalOrder = array(4);

if(in_array($_SESSION['sid'],$showTotalOrder) && $time_mark){
    $count_total=$pOrder->totalOrders(2);
    $count_total = explode(',',$count_total);
}
if ($_GET['cgp']) {
    print_r($r);exit;
}

include("tpl/orderlist_dev_new.html");
include("common/footer.html");
?>